<!DOCTYPE html>
<html>
<head lang="en">
	<meta charset="UTF-8">
<title>带关闭按钮的滚动广告</title>
<style type="text/css">
#main{ text-align:center;}
#float{
	position:absolute;
	left:30px;
	top:60px; 
	z-index:1;
	}
#close{
	position:absolute;
	top:60px;
	left:134px;
	z-index:2;
	cursor:hand;
}
</style>
</head>
<body>
<div id="close" onClick="adv_close()"><img src="images/close.jpg"></div>
<div id="float"><img src="images/advpic.jpg"></div>
<div id="main"><img src="images/contentpic.jpg"></div>
<script>
	var floatObj = document.getElementById("float");
	var closeObj = document.getElementById("close"); 

	// 定位时 距顶部和左侧的距离
	var closeTop;
	var closeLeft;

	var floatTop;
	var floatLeft;

	// 关闭广告
	function adv_close(){
		floatObj.style.display = "none";
		closeObj.style.display = "none";
	}

	function init(){
		// 非ie浏览器js获取css属性值
		floatTop = document.defaultView.getComputedStyle(floatObj,null).top;  // 60px
		floatLeft = document.defaultView.getComputedStyle(floatObj,null).left; // 30px

		closeTop = document.defaultView.getComputedStyle(closeObj,null).top;
		closeLeft = document.defaultView.getComputedStyle(closeObj,null).left;
	}
	init();

	// 监听滚动条移动
	function move(){
		var top = document.documentElement.scrollTop || document.body.scrollTop;
		floatObj.style.top = top + parseInt(floatTop) + "px";
		closeObj.style.top = top + parseInt(closeTop) + "px";
	}

	window.onscroll = move;
</script>
</body>
</html>
